import {Injectable} from '@nestjs/common';
import {Response} from 'express';
import * as svgCaptcha from "svg-captcha";
import * as md5 from "md5";

interface ArgType {
    type?: string,
    redirectUrl?: string,
    message?: string,
}

@Injectable()
export class ToolsService {
    // 获取图形验证码
    getCaptcha() {
        return svgCaptcha.create({
            size: 4,
            fontSize: 50,
            width: 100,
            height: 36,
            background: '#fff3cd',
            ignoreChars: '0o1i',
            noise: 2
        })
    }

    // Md5加密
    getMd5(str) {
        return md5(str)
    }

    // 公共提示页面
    async tips(res: Response, arg: ArgType) {
        const {type = '', redirectUrl = '', message = '回到上一个页面'} = arg
        await res.render(`public/${type}`, {redirectUrl, message})
    }
}
